package com.uchedao.seller.http.request;

import android.text.TextUtils;

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
import com.uchedao.seller.SellerApplication;
import com.uchedao.seller.config.Api;
import com.uchedao.seller.manager.AppInfoManager;
import com.uchedao.seller.http.response.ResponseData;
import com.uchedao.seller.util.LogUtil;
import com.uchedao.seller.util.T;

import java.io.UnsupportedEncodingException;

/**
 * Created by zhengxizhen on 15/8/19.
 */
public class GetUrlRequest extends Request<JsonObject> {

    private final Response.Listener<JsonObject> mListener;

    private Gson mGson;


    public GetUrlRequest(String action, Response.Listener<JsonObject> listener,
                         Response.ErrorListener errorListener) {
        super(Method.GET, Api.Net.HOST + action, errorListener);
        LogUtil.e("ACTION GET >>>>>>>>>>>>>>>>>>>>>>>>" + Api.Net.HOST + action);
        mGson = new Gson();
        mListener = listener;
    }

    @Override
    protected void deliverResponse(JsonObject response) {
        mListener.onResponse(response);
    }

    @Override
    protected Response<JsonObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
            LogUtil.e("jsonString", ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n" + jsonString + "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
            ResponseData responseData = mGson.fromJson(jsonString, ResponseData.class);
            String result = responseData.getError_code();
            if (TextUtils.isEmpty(result)) {
                LogUtil.e("resolverData", "返回结果不是合法的json格式");
                return Response.error(new VolleyError("返回结果不是合法的json格式"));
            }
            if ("0".equals(result)) {
                return Response.success(responseData.getData(),
                        HttpHeaderParser.parseCacheHeaders(response));
            } else {
                LogUtil.e("resolverData", responseData.getMessage());

                if ("40002".equals(result)) {
                    // token 过期处理 重新获取
//                    Toast.makeText(AuctionApplication.getAppContext()
//                            , AuctionApplication.getAppContext().getString(R.string.token_is_error)
//                            , Toast.LENGTH_SHORT).show();
                    LogUtil.e("授权过期！");
//                    AppInfoManager.saveSTATE(false);
//                    // 重新启动主页，重新加载token
//                    Intent intent = new Intent(SellerApplication.getAppContext(), WelComeActivity.class);
//                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                    AuctionApplication.getAppContext().startActivity(intent);
                    AppInfoManager.syncToken();
                    T.showNetworkError(SellerApplication.getContext());
                } else if ("40003".equals(result)) {
                    //账号登录失效，重新登录
//                    UserInfoManager.saveSTATE(false);
//                    Intent intent = new Intent(SellerApplication.getContext(), LoginActivity.class);
//                    SellerApplication.getContext().startActivity(intent);
                }
                return Response.error(new VolleyError(responseData.getMessage()));
            }

        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JsonSyntaxException je) {
            return Response.error(new ParseError(je));
        }
    }

}